package org.gp.Interfaz;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JButton;

import org.gp.tringes.GestorIGInicio;
import org.gp.tringes.GestorIGRegistroClub;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.SQLException;


public class IGModifContraClub extends JDialog{
	
	private Integer idClub;
	
	private JPanel panel;
	private JPanel panel_1;
	private JPanel panel_2;
	private JLabel lblTitulo;
	private JPasswordField txtContrasea;
	private JLabel lblAviso;
	private JPasswordField txtRepetirContrasea;
	private JButton btnAceptar;
	private JButton btnCancelar;
	
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					IGModifContraClub dialog = new IGModifContraClub(1234567);
					dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
					dialog.setVisible(true);
					dialog.setModal(true);
					dialog.setResizable(false);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	
	public IGModifContraClub(Integer pIdClub) {
		this.idClub = pIdClub;
		
		setBounds(100, 100, 250, 175);
		//ID-177
		setTitle(GestorIGInicio.getGestorIGInicio().getTexto(177));
		
		getContentPane().setLayout(new BorderLayout(0, 0));
		getContentPane().add(getPanel(), BorderLayout.NORTH);
		getContentPane().add(getPanel_1(), BorderLayout.CENTER);
		getContentPane().add(getPanel_2(), BorderLayout.SOUTH);
		
	}
	
	private JPanel getPanel(){
		if(panel == null){
			panel = new JPanel();
			panel.add(getLblTitulo());
		}
		return panel;
	}
	private JLabel getLblTitulo() {
		if (lblTitulo == null) {
			//ID-178
			lblTitulo = new JLabel(GestorIGInicio.getGestorIGInicio().getTexto(178));
		}
		return lblTitulo;
	}
	
	private JPanel getPanel_1(){
		if(panel_1 == null){
			panel_1 = new JPanel();
			panel_1.add(getTxtContrasea());
			panel_1.add(getLblAviso());
			panel_1.add(getTxtRepetirContrasea());
		}
		return panel_1;
	}
	private JPasswordField getTxtContrasea() {
		if (txtContrasea == null) {
			txtContrasea = new JPasswordField();
			txtContrasea.setColumns(10);
		}
		return txtContrasea;
	}
	private JLabel getLblAviso() {
		if (lblAviso == null) {
			//ID-179
			lblAviso = new JLabel(GestorIGInicio.getGestorIGInicio().getTexto(179));
		}
		return lblAviso;
	}
	private JPasswordField getTxtRepetirContrasea() {
		if (txtRepetirContrasea == null) {
			txtRepetirContrasea = new JPasswordField();
			txtRepetirContrasea.setColumns(10);
		}
		return txtRepetirContrasea;
	}
	
	private JPanel getPanel_2(){
		if(panel_2 == null){
			panel_2 = new JPanel();
			panel_2.add(getBtnAceptar());
			panel_2.add(getBtnCancelar());
		}
		return panel_2;
	}
	private JButton getBtnAceptar() {
		if (btnAceptar == null) {
			//ID-2
			btnAceptar = new JButton(GestorIGInicio.getGestorIGInicio().getTexto(2));
			btnAceptar.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseClicked(MouseEvent arg0) {
					try{
						Boolean compCont = GestorIGRegistroClub.getGestorIGRegistroClub().comprobarContrasea(txtContrasea.getPassword(), txtRepetirContrasea.getPassword());
						if(compCont){
							//Si = 0 , No = 1
							//ID-180 y 4
							int opcion = JOptionPane.showConfirmDialog(null, GestorIGInicio.getGestorIGInicio().getTexto(180), GestorIGInicio.getGestorIGInicio().getTexto(4), JOptionPane.YES_NO_OPTION);
							if(opcion == 0){
								String contrasea = String.valueOf(txtContrasea.getPassword());
								GestorIGRegistroClub.getGestorIGRegistroClub().actualizarDatos(contrasea, "contrasenya", idClub);
								//ID-68 y 6
								JOptionPane.showMessageDialog(null, GestorIGInicio.getGestorIGInicio().getTexto(68), GestorIGInicio.getGestorIGInicio().getTexto(6), JOptionPane.INFORMATION_MESSAGE);
								dispose();
							}
						}else{
							//ID-181 y 4
							JOptionPane.showMessageDialog(null, GestorIGInicio.getGestorIGInicio().getTexto(181), GestorIGInicio.getGestorIGInicio().getTexto(4), JOptionPane.WARNING_MESSAGE);
						}
					} catch (SQLException e) {
					e.printStackTrace();
					}
				}
			});
		}
		return btnAceptar;
	}
	private JButton getBtnCancelar() {
		if (btnCancelar == null) {
			//ID-8
			btnCancelar = new JButton(GestorIGInicio.getGestorIGInicio().getTexto(8));
			btnCancelar.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseClicked(MouseEvent arg0) {
					dispose();
				}
			});
		}
		return btnCancelar;
	}
}
